#!/bin/bash
# this script tests the examples 1-4 from cgconfig.conf man pages
# - for each one tests whether the configuration which is created using described configuration
# file is identical with the example described below
# TODO: Example 5 is missing now


. `dirname $0`/../testenv.sh

CONFDIR=$TESTDIR/tools/cgconfigparser/cgconfigparser_conf_files

# all mount points are in /$TMP/cgroups
mkdir /$TMP/cgroups/ 2>/dev/null
#-----------------------------------------------------------------------------
# EXAMPLE 1
#-----------------------------------------------------------------------------
$TOOLSDIR/cgconfigparser -l `prepare_config $CONFDIR/cgconfig.conf.5.ex1` || \
	die "cgconfigparser -l $CONFDIR/cgconfig.conf.5.ex1 failed"

$TOOLSDIR/cgsnapshot  -b $CONFIGDIR/cgsnapshot_blacklist.conf -s \
	 >/$TMP/temp.conf || die "example1 cgsnapshot failed"

lssubsys_1=`$TOOLSDIR/lssubsys -m`

$TOOLSDIR/cgclear || \
	die "example1 1st cgclear failed"

# cgsnapshot sanity test
$TOOLSDIR/cgconfigparser -l /$TMP/temp.conf || \
	die "example1 cgconfigparser failed"

lssubsys_2=`$TOOLSDIR/lssubsys -m`

$TOOLSDIR/cgclear || \
	die "example1 2st cgclear failed"

if [ "$lssubsys_1" != "$lssubsys_2" ]; then
    echo "ERROR(example1): lssubsys_1='$lssubsys_1', lssubsys_2='$lssubsys_2'"
    die "example1 failed"
fi

# cgconfigparser test
mkdir /$TMP/cgroups/cpu 2>/dev/null
mount -t cgroup -o cpu,cpuacct cpu $TMP/cgroups/cpu || die "example1 Mount failed"
lssubsys_3=`lssubsys -m`
$TOOLSDIR/cgclear || die "example1 3rd cgclear failed"

if [ "$lssubsys_1" != "$lssubsys_3" ]; then
    echo "ERROR(example1): lssubsys_1='$lssubsys_1', lssubsys_3='$lssubsys_3'"
    die "example1 failed"
fi

#-----------------------------------------------------------------------------
# EXAMPLE 2
#-----------------------------------------------------------------------------
$TOOLSDIR/cgconfigparser -l `prepare_config $CONFDIR/cgconfig.conf.5.ex2` || \
    die "cgconfigparser -l $CONFDIR/cgconfig.conf.5.ex2 failed"

val1=`$TOOLSDIR/cgget -nv -r cpu.shares daemons/ftp`;
val2=`$TOOLSDIR/cgget -nv -r cpu.shares daemons/www`;


if [ $val1 -ne 500 ]; then
    echo "ERROR(example2): bogus variable setting daemons/ftp/cpu.shares $val1 "\
        "(should be 500)"
    die "example2 failed"
fi

if [ $val2 -ne 1000 ]; then
    echo "ERROR(example2): bogus variable setting daemons/www/cpu.shares $val2 "\
        "(should be 1000)"
    die "example2 failed"
fi

$TOOLSDIR/cgsnapshot  -b $CONFIGDIR/cgsnapshot_blacklist.conf -s >\
	/$TMP/temp.conf || die "example2 cgsnapshot failed"

lssubsys_1=`$TOOLSDIR/lssubsys -m`
$TOOLSDIR/cgclear || die "example2 1st cgclear failed"


# cgsnapshot sanity test
$TOOLSDIR/cgconfigparser -l /$TMP/temp.conf || \
	die "example2 cgconfigparser of cgsnapshot file failed"

lssubsys_2=`$TOOLSDIR/lssubsys -m`
val1=`$TOOLSDIR/cgget -nv -r cpu.shares daemons/ftp`;
val2=`$TOOLSDIR/cgget -nv -r cpu.shares daemons/www`;
$TOOLSDIR/cgclear || die "example2 2st cgclear failed"

if [ "$lssubsys_1" != "$lssubsys_2" ]; then
    echo "ERROR(example2): lssubsys_1='$lssubsys_1', lssubsys_2='$lssubsys_2'"
    die "example2 failed"
fi


if [ $val1 -ne 500 ]; then
    echo "ERROR(example2): bogus variable setting daemons/ftp/cpu.shares $val1 "\
    "(should be 500)"
   die "example2 failed"
fi

if [ $val2 -ne 1000 ]; then
    echo "ERROR(example2): bogus variable setting daemons/www/cpu.shares $val2 "\
    "(should be 1000)"
    die "example2 failed"
fi

# cgconfigparser test
mkdir /$TMP/cgroups/cpu 2>/dev/null
mount -t cgroup -o cpu,cpuacct cpu /$TMP/cgroups/cpu ||
    die "example2 mount failed"
mkdir /$TMP/cgroups/cpu/daemons 2>/dev/null

mkdir /$TMP/cgroups/cpu/daemons/www 2>/dev/null
echo 1000 > /$TMP/cgroups/cpu/daemons/www/cpu.shares ||
    die "example2 var.setting failed"

mkdir /$TMP/cgroups/cpu/daemons/ftp 2>/dev/null
echo 500 > /$TMP/cgroups/cpu/daemons/ftp/cpu.shares ||
    die "example2 var.setting failed"

lssubsys_3=`$TOOLSDIR/lssubsys -m`
$TOOLSDIR/cgclear || die "example2 3rd cgclear failed"


if [ "$lssubsys_1" != "$lssubsys_3" ]; then
    echo "ERROR(example1): lssubsys_1='$lssubsys_1', lssubsys_3='lssubsys_3'"
    die "example2 failed"
fi

#-----------------------------------------------------------------------------
# EXAMPLE 3
#-----------------------------------------------------------------------------
$TOOLSDIR/cgconfigparser -l `prepare_config $CONFDIR/cgconfig.conf.5.ex3` || \
    die "cgconfigparser -l $CONFDIR/cgconfig.conf.5.ex3 failed"

$TOOLSDIR/cgsnapshot -b $CONFIGDIR/cgsnapshot_blacklist.conf  \
    -s >/$TMP/temp.conf || die "example3 cgsnapshot failed"
lssubsys_1=`$TOOLSDIR/lssubsys -m`
lscg1=`$TOOLSDIR/lscgroup`
$TOOLSDIR/cgclear || die "example3 1st cgclear failed"

# cgsnapshot sanity test
$TOOLSDIR/cgconfigparser -l /$TMP/temp.conf || die "example3 cgconfigparser failed"
lssubsys_2=`$TOOLSDIR/lssubsys -m`
lscg2=`$TOOLSDIR/lscgroup`
$TOOLSDIR/cgclear || die "example3 2st cgclear failed"

if [ "$lssubsys_1" != "$lssubsys_2" ]; then
    echo "ERROR(example3): lssubsys_1='$lssubsys_1', lssubsys_='$lssubsys_2'"
    die "example3 failed"
fi


if [ "$lscg1" != "$lscg2" ]; then
    echo "ERROR(example3): lscg1='$lscg1', lscg2='$lscg2'"
    die "example3 failed"
fi

# cgconfigparser test
mkdir /$TMP/cgroups/cpu 2>/dev/null
mkdir /$TMP/cgroups/cpuacct 2>/dev/null
mount -t cgroup -o cpu cpu /$TMP/cgroups/cpu || die "example3 Mount1 failed"
mount -t cgroup -o cpuacct cpuacct /$TMP/cgroups/cpuacct || die "example3 Mount2 failed"

mkdir /$TMP/cgroups/cpu/daemons 2>/dev/null
mkdir /$TMP/cgroups/cpuacct/daemons 2>/dev/null

lssubsys_3=`$TOOLSDIR/lssubsys -m`
lscg3=`$TOOLSDIR/lscgroup`
$TOOLSDIR/cgclear || die "example3 3rd cgclear failed"

if [ "$lssubsys_1" != "$lssubsys_3" ]; then
    echo "ERROR(example3): lssubsys_1='$lssubsys_1', lssubsys_3='$lssubsys_3'"
    die "example3 failed"
fi

if [ "$lscg1" != "$lscg3" ]; then
    echo "ERROR(example3): lscg1='$lscg1', lscg3='$lscg3'"
    die "example3 failed"
fi

#-----------------------------------------------------------------------------
# EXAMPLE 4
#-----------------------------------------------------------------------------
$TOOLSDIR/cgconfigparser -l `prepare_config $CONFDIR/cgconfig.conf.5.ex4` || \
    die "cgconfigparser -l $CONFDIR/cgconfig.conf.5.ex4 failed"

$TOOLSDIR/cgsnapshot -b $CONFIGDIR/cgsnapshot_blacklist.conf -s \
    >/$TMP/temp.conf || die "example4 cgsnapshot failed"
lssubsys_1=`$TOOLSDIR/lssubsys -m`
lscg1=`$TOOLSDIR/lscgroup | sort`
$TOOLSDIR/cgclear || die "example4 1st cgclear failed"

# cgsnapshot sanity test
$TOOLSDIR/cgconfigparser -l /$TMP/temp.conf || die "example4 cgconfigparser failed"
lssubsys_2=`$TOOLSDIR/lssubsys -m`
lscg2=`$TOOLSDIR/lscgroup| sort`
$TOOLSDIR/cgclear || die "example4 2st cgclear failed"

if [ "$lssubsys_1" != "$lssubsys_2" ]; then
    echo "ERROR(example4): lssubsys_1='$lssubsys_1', lssubsys_2='$lssubsys_2'"
    die "example4 failed"
fi

if [ "$lscg1" != "$lscg2" ]; then
    echo "ERROR(example4): lscg1='$lscg1', lscg2='$lscg2'"
    die "example4 failed"
fi

# cgconfigparser test
mkdir /$TMP/cgroups/cpu 2>/dev/null
mkdir /$TMP/cgroups/cpuacct 2>/dev/null
mount -t cgroup -o cpu cpu /$TMP/cgroups/cpu || die "example4 Mount1 failed"
mount -t cgroup -o cpuacct cpuacct /$TMP/cgroups/cpuacct || die "example4 Mount2 failed"

mkdir /$TMP/cgroups/cpuacct/daemons 2>/dev/null
mkdir /$TMP/cgroups/cpu/daemons 2>/dev/null
mkdir /$TMP/cgroups/cpu/daemons/www 2>/dev/null
mkdir /$TMP/cgroups/cpu/daemons/ftp 2>/dev/null

lssubsys_3=`$TOOLSDIR/lssubsys -m`
lscg3=`$TOOLSDIR/lscgroup | sort`
cgclear || die "example3 3rd cgclear failed"


if [ "$lssubsys_1" != "$lssubsys_3" ]; then
    echo "ERROR(example4): lssubsys_1='$lssubsys_1', lssubsys_3='$lssubsys_3'"
    die "example4 failed"
fi

if [ "$lscg1" != "$lscg3" ]; then
    echo "ERROR(example4): lscg1='$lscg1', lscg3='$lscg3'"
    die "example4 failed"
fi


cleanup
exit 0